package service

import (
	"vrcm/src/vrcm/app/admin/models/monitor"
	dto "vrcm/src/vrcm/app/admin/service/dto/monitor"
	cDto "vrcm/src/vrcm/common/dto"
	"vrcm/src/vrcm/common/service"
)

type SetManage struct {
	service.Service
}

// GetPage 获取MonitorBasic列表
func (e *SetManage) GetPage(c *dto.SetManageGetPageReq, list *[]monitor.SetManage, count *int64) error {
	var err error
	var data monitor.SetManage

	err = e.Orm.Model(&data).
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("GetPage error:%s \r\n", err)
		return err
	}
	//configIds := make([]int, 0, 5)
	//for _, v := range *list {
	//	v := v
	//	configIds = append(configIds, v.Id)
	//}
	//configs := make([]monitor.MonitorConfig, 0, 5)
	//err = e.Orm.Model(&monitor.MonitorConfig{}).Where("basic_id in ?", configIds).Find(&configs).Error
	//if err != nil {
	//	e.Log.Errorf("get MonitorConfig error:%s \r\n", err)
	//	return err
	//}
	//
	//configMap := make(map[int64][]monitor.MonitorConfig)
	//for _, v := range configs {
	//	v := v
	//	t, ok := configMap[v.BasicId]
	//	if !ok {
	//		t = make([]monitor.MonitorConfig, 0, 5)
	//	}
	//	t = append(t, v)
	//	configMap[v.BasicId] = t
	//}
	//for k, v := range *list {
	//	v := v
	//	configs := configMap[int64(v.Id)]
	//	*(&(*list)[k].MonitorConfigs) = configs
	//}
	return nil
}

// Get 获取SetManage对象
func (e *SetManage) Get(d *dto.SetManageGetReq) (*monitor.SetManage, error) {
	var model monitor.SetManage
	err := e.Orm.Model(&model).Where("id=?", d.Id).Find(&model).Error
	if err != nil {
		e.Log.Errorf("get error:%s", err)
		return &model, err
	}
	return &model, nil
}

func (e *SetManage) Insert(c *dto.SetMangeInsertReq) error {
	var err error
	var data monitor.SetManage
	data.TenantId = c.TenantId
	data.SetName = c.SetName
	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("创建片场:%s \r\n", err)
		return err
	}
	c.Id = data.Id
	return nil
}

func (e *SetManage) Update(c *dto.SetMangeUpdateReq) error {

	var err error
	var data monitor.SetManage
	err = e.Orm.Model(&monitor.SetManage{}).Where("id=?", c.Id).Find(&data).Error
	if err != nil {
		e.Log.Errorf("get SetManage:%s \r\n", err)
		return err
	}

	data.TenantId = c.TenantId
	data.SetName = c.SetName
	err = e.Orm.Save(&data).Error
	if err != nil {
		e.Log.Errorf("更新片场失败:%s \r\n", err)
		return err
	}
	c.Id = data.Id
	return nil
}

func (e *SetManage) Remove(d *dto.SetManageDeleteReq) error {
	err := e.Orm.Model(&monitor.SetManage{}).Where("id in ?", d.Ids).Delete(&monitor.SetManage{}).Error
	if err != nil {
		e.Log.Errorf("删除片场失败:%s \r\n", err)
		return err
	}
	return nil
}

func (e *SetManage) GetEvent(c *dto.GetSetReq) (monitor.MonitorEvent, error) {

	var err error
	var set monitor.SetManage

	event := monitor.MonitorEvent{}
	sets := make([]monitor.SetManage, 0, 5)
	setId := 0
	if c.Id <= 0 {
		err = e.Orm.Model(&monitor.SetManage{}).Where("tenant_id=?", c.TenantId).Find(&sets).Error
		if err != nil {
			e.Log.Errorf("get SetManage:%s \r\n", err)
			return event, err
		}
		if len(sets) > 0 {
			setId = sets[0].Id
		}
	} else {
		err = e.Orm.Model(&monitor.SetManage{}).Where("id=?", c.Id).Find(&set).Error
		if err != nil {
			e.Log.Errorf("get SetManage:%s \r\n", err)
			return event, err
		}
		setId = set.Id
	}
	err = e.Orm.Model(&monitor.MonitorEvent{}).Where("task_state=1 and set_id=?", setId).Find(&event).Error
	if err != nil {
		e.Log.Errorf("get MonitorEvent:%s \r\n", err)
		return event, err
	}

	return event, nil
}
